package sopt.studyEng;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import statics.SoptVars;

import comm.parsers.DaumSearchSAXParserHandler;

public class DaumAPIProc {
	public static ArrayList getResultDaumDicSearch(String q) throws UnsupportedEncodingException{
		ArrayList lt = null;
		System.out.println("=========들어온 쿼리 값");
		System.out.println(q);
		
		String reqUrl = "http://apis.daum.net/dic/endic?apikey="+SoptVars.DaumDicAPIKEY+"&output=xml&kind=WORD&q="+URLEncoder.encode(q, "UTF-8");

		try {
			SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
			
			DaumSearchSAXParserHandler xmlHandler = new DaumSearchSAXParserHandler();
			
			parser.parse(reqUrl, xmlHandler);
			
			lt = xmlHandler.getItemList();
		} catch (ParserConfigurationException e) {
			return lt;
		} catch (IOException e) {
			return lt;
		} catch (SAXException e) {
			return lt;
		}
		
		System.out.println("DAUM API가 리턴하는값 ");
		System.out.println(lt.toString());
		
		return lt;
	}
}
